package com.mdc.mansion.web.modules.system.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mdc.mansion.core.modules.system.SysDictService;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.system.entity.Dict;
import com.mdc.mansion.web.modules.system.service.SystemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mdc.mansion.web.common.web.BaseController;

/**
 * 字典Controller
 * @author chenmingdong
 * @version 2016-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/system/dict")
public class DictController extends BaseController {

	@Autowired
	private SysDictService sysDictService;
	
	@Autowired
	private SystemService systemService;
	
	@ModelAttribute
	public Dict get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getDict(id);
		}else{
			return new Dict();
		}
	}
	
	@RequiresPermissions("system:dict:view")
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(Dict dict) {
		Map<String, Object> model=new HashMap<String, Object>();
		List<String> typeList = sysDictService.findTypeList();
		Page<Dict> page = sysDictService.findPage(dict);
		model.put("typeList", typeList);
        model.put("page", page);
		ModelAndView modelAndView=new ModelAndView("modules/system/dictList", model);
		return modelAndView;
	}
	
	@RequiresPermissions("system:dict:view")
	@RequestMapping(value = "form")
	public ModelAndView form(Dict dict) {
		Map<String, Object> model=new HashMap<String, Object>();
		model.put("dict", dict);
		return new ModelAndView("modules/system/dictForm",model);
	}
	

	@RequiresPermissions("system:dict:edit")
	@RequestMapping(value = "save")
	public ModelAndView edit(Dict dict,RedirectAttributes redirectAttributes) {
		systemService.saveDict(dict);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
	@RequiresPermissions("system:dict:edit")
	@RequestMapping(value = "delete")
	public ModelAndView delete(Dict dict,RedirectAttributes redirectAttributes) {
		systemService.deleteDict(dict);
		this.addMessage(redirectAttributes, "操作成功");
		return new ModelAndView("redirect:./list");
	}
	
}
